<template>
  <el-row loading>
    <el-col :span="24">
       <truckType-edit ref="truckTypeEdit"  :parentGoodsType="parentGoodsType" v-on:save-finished="queryAll()"></truckType-edit>
      <el-row>
        <el-col :span="6">
          <el-input placeholder="请输入车型名称" size="small" v-model="search.name" style="width:100%">
          </el-input>
        </el-col>
        <el-col :span="6">
           <el-input placeholder="请输入车型载重" size="small" v-model="search.allowableLoad" style="width:100%">
          </el-input>
        </el-col>
        <el-col :span="6">
           <el-input placeholder="请输入车型体积" size="small" v-model="search.allowableVolume" style="width:100%">
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="danger"  size="small" @click="queryAll()">搜索</el-button>
        </el-col>
        <el-col :span="24" style="text-align: right;">
          <el-button  icon="el-icon-plus" type="danger" @click="handleCreatetruckType()" v-show="add">新增车型</el-button>
        </el-col>
      </el-row>
      </el-col>
       <el-col :span="24">
          <el-table :data="tableData" border style="width: 100%">
            <el-table-column prop="id" label="车型编号" header-align="center"></el-table-column>
            <el-table-column prop="name" label="车型名称" header-align="center"></el-table-column>
            <el-table-column prop="allowableLoad" label="车型载重(千克)" header-align="center" width="120"></el-table-column>
            <el-table-column prop="allowableVolume" label="准载体积(方)" header-align="center" width="120"></el-table-column>
            <el-table-column prop="measureLong" label="长(米)" header-align="center" align="center"></el-table-column>
            <el-table-column prop="measureWidth" label="宽(米)" header-align="center" align="center"></el-table-column>
            <el-table-column prop="measureHigh" label="高(米)" header-align="center" align="center"></el-table-column>
            <el-table-column   label="适用货品类型" header-align="center" align="center" width="120">
              <template slot-scope="scope">
                  <span v-for="item in parentGoodsType"  v-if="item.id==scope.row.goodsTypeIds">{{item.name}}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" header-align="center" align="center" width="200">
              <template slot-scope="scope">
                <el-button v-show="edit" type="primary" size="mini"
                  @click="handleEdittruckType(scope.row)">编辑</el-button>
                <el-button  type="danger" size="mini"
                  @click="handleDeletetruckType(scope.row.id)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-row>
        <el-col :span="24">
         <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="currentChange"
            :current-page.sync="search.page"
            :page-sizes="[2,4,6,8]"
            :page-size="search.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="counts">
          </el-pagination>
        </el-col>
      </el-row>
        </el-col>
</el-row>
</template>

<script>
import modelsApi from '@/api/models.js'
import TruckTypeEdit from '@/components/truckType/truckTypeEdit'
export default {
    data() {
      return {
        add:true,
       edit:true,
       parentGoodsType:[],
       tableData:[],
        loading: true,
        search:{
        name:"",
        allowableLoad:"",
        allowableVolume:"",
        pageSize:2,//每页显示的条数
        page:1,//第一页
        },
        pages:"",//总共几页
        counts:0 //共几条
      }
    },
    methods: {
      queryAll(){
        modelsApi.queryAll(this.search)
        .then(res => {
          this.tableData=res.data.items
          this.counts=res.data.counts;
          console.log(this.tableData)
        })
        .catch(err => {
          console.error(err);
        })
      },
      //查询所有货物类型
      findall(){
        modelsApi.findall().then(res =>{
          this.parentGoodsType=res.data
        })
      },
      currentChange(val){
        this.page=val;
        this.queryAll();
      },
      handleSizeChange(val) {
        this.pageSize=val;
        this.queryAll();
        },
      handleCreatetruckType(){
        this.loading = true
        this.$refs.truckTypeEdit.$emit('openAddTruckTypeDialog')
        this.loading = false
      },
      handleEdittruckType(row) {
      this.loading = true
      console.log(row)
      modelsApi.update(row.id,row)
      .then(response => {
        this.$refs.truckTypeEdit.$emit('openEditTruckTypeDialog',response.data)
        this.loading = false
      })
      .catch(error => {
        console.log(error)
      })
    },
    handleDeletetruckType(id){
       this.$confirm('确定删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      .then(() => {
        modelsApi.delete(id)
        .then(res => {
         if(res.data.msg=="success"){
            this.$message({
            message: '删除了一条记录',
            type: 'warning'
        })
        this.queryAll()
         }
        })
    })
 }
},
    created(){
        this.queryAll();
        this.findall();
    },
    components:{TruckTypeEdit},

  }

</script>
